package com.jinghangzz.shop.common.util;

/**
 * 分页的工具类
 * @author TeaBig
 */
public class PageInfoUtil
{
	/* 每页多少条 */
	private int pageSize = 20 ;
	/* 总条数 */
	private int totalRecord ;
	/* 总页数 */
	private int totalPage ; 
	/* 当前页 */
	private int currentPage ;
	/* 上一页 */
	private int prePage ; 
	/* 下一页 */
	private int nextPage ;

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public int getTotalRecord()
	{
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}

	/**
	 * 获取当前页
	 * @return
	 */
	public int getCurrentPage()
	{
		if(this.currentPage < 1)
		{
			this.currentPage = 1 ; 
		}
		/* 如果设置的当前页大于总页数
		 * 如果数据库表里面的记录就是0;
		 * 总页数:0,或者是1;
		 * 设置当前页设置成200,
		 * 应该把当前页变成0,还是1
		 *  */
		if(this.currentPage > this.getTotalPage() && this.getTotalRecord() > 0 )
		{
			this.currentPage = this.getTotalPage() ; 
		}
		return currentPage;
	}
	
	/**
	 * 设置当前页
	 * -100
	 * 200000
	 * @param currentPage
	 */
	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}

	public int getTotalPage()
	{
		/* 总页数 */
		this.totalPage = this.totalRecord / this.pageSize ; 
		if(this.totalRecord % this.pageSize != 0 )
		{
			/* 有余数 */
			this.totalPage = this.totalPage + 1 ; 
		}
		return totalPage;
	}

	public int getPrePage()
	{
		/* 上一页 */
		this.prePage = this.currentPage - 1; 
		if(this.prePage < 1)
		{
			this.prePage = 1 ; 
		}
		return prePage;
	}

	public int getNextPage()
	{
		/* 下一页 */
		this.nextPage = this.currentPage + 1 ; 
		if(this.nextPage > this.getTotalPage() && this.getTotalRecord() > 0 )
		{
			this.nextPage = this.getTotalPage() ; 
		}
		return nextPage;
	}

	@Override
	public String toString()
	{
		return "PageInfoUtil [pageSize=" + pageSize + ", totalRecord=" + totalRecord + ", totalPage=" + this.getTotalPage()
				+ ", currentPage=" + this.getCurrentPage() + ", prePage=" + this.getPrePage() + ", nextPage=" + this.getNextPage() + "]";
	}
	
	public static void main(String[] args)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		pageInfoUtil.setTotalRecord(30);
		pageInfoUtil.setPageSize(10);
		pageInfoUtil.setCurrentPage(-1000);
		System.out.println("---->" + pageInfoUtil);
	}
}
